import 'package:flutter/material.dart';

class SortAndSearchBar extends StatelessWidget {
  final String currentSort;
  final Function(String) onSortChanged;
  final Function(String) onSearch;

  const SortAndSearchBar({
    super.key,
    required this.currentSort,
    required this.onSortChanged,
    required this.onSearch,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 左边：排序按钮列
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildSortItem("createdAt", "最新"),
              const SizedBox(height: 6),
              _buildSortItem("hot", "最热"),
            ],
          ),

          const SizedBox(width: 12),

          // 右边：搜索框
          Expanded(
            child: TextField(
              onSubmitted: onSearch,
              decoration: InputDecoration(
                prefixIcon: const Icon(Icons.search),
                hintText: "搜索帖子标题或关键词",
                isDense: true,
                contentPadding: const EdgeInsets.symmetric(
                  vertical: 12,
                  horizontal: 12,
                ),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildSortItem(String value, String label) {
    return GestureDetector(
      onTap: () => onSortChanged(value),
      child: Row(
        children: [
          Icon(
            currentSort == value
                ? Icons.radio_button_checked
                : Icons.radio_button_unchecked,
            color: currentSort == value ? Colors.blue : Colors.grey,
            size: 20,
          ),
          const SizedBox(width: 4),
          Text(label, style: const TextStyle(fontSize: 14)),
        ],
      ),
    );
  }
}
